from sys import exit
from bitcoin.core.script import *
from bitcoin.wallet import CBitcoinSecret
from utils import *
from config import my_private_key, my_public_key, my_address, faucet_address
from ex1 import send_from_P2PKH_transaction


cust1_private_key = CBitcoinSecret(
    'cQjR17c4BRt7wmk1ezNucUjM4xBQU2mMKoggdvYpxs2uWaWF2t4y')
cust1_public_key = cust1_private_key.pub
cust2_private_key = CBitcoinSecret(
    'cSrwUVm8TxhghSZv8oASs2AMES2vYo8cbtPmLVZePihT1jkfnZMn')
cust2_public_key = cust2_private_key.pub
cust3_private_key = CBitcoinSecret(
    'cPtrDLEVgJ2bdPpQjRCLFoKj2aRLa1QAQ2pgGX7m7oSJ2gErvpfg')
cust3_public_key = cust3_private_key.pub


######################################################################
# TODO: Complete the scriptPubKey implementation for Exercise 2

# You can assume the role of the bank for the purposes of this problem
# and use my_public_key and my_private_key in lieu of bank_public_key and
# bank_private_key.

ex2a_txout_scriptPubKey = [ #多签名输出脚本
    my_public_key,  #银行公钥
    OP_CHECKSIGVERIFY,  #验证签名的脚本，它检查签名是否与公钥匹配
    OP_1,   #至少还需要1个签名
    cust1_public_key,
    cust2_public_key,
    cust3_public_key,
    OP_3,   #指定用户公钥的总数
    OP_CHECKMULTISIG    #检查提供的签名数量是否满足要求（至少1个），并且这些签名是否与给定的公钥匹配
]

######################################################################

if __name__ == '__main__':
    ######################################################################
    # TODO: set these parameters correctly
    amount_to_send = 0.0000001
    txid_to_spend = (
        '4e506e7540ec8931641112b53b74e4fa1f51aa1d00d41e12bba5dd0afcc047ca') #打算花的比特币来自分币那笔交易
    utxo_index = 2
    ######################################################################

    response = send_from_P2PKH_transaction( #单签名
        amount_to_send, txid_to_spend, utxo_index,
        ex2a_txout_scriptPubKey)
    print(response.status_code, response.reason)
    print(response.text)
